package sg.edu.nus.dashboard.service.baseline;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import sg.edu.nus.dashboard.service.sqlexecutor.SQLService;
import sg.edu.nus.dashboard.vo.BaselineVo;

public class BaselineService {

	public boolean configBaseline(BaselineVo baselineVo) {
		System.out.println("configBaseline()");
		try {
			String sql = this.constructUpdateBaseLineSQL(baselineVo);
			System.out.println(sql);
			SQLService s = new SQLService();
			boolean result = s.executeUpdate(sql);

			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public List<BaselineVo> readBaseline() {
		System.out.println("readBaseline()");
		// TODO read the baseline
		try {
			List<BaselineVo> list = new ArrayList<BaselineVo>();
			String sql = this.contructSelectBaseLineSQL();
			System.out.println(sql);
			SQLService s = new SQLService();
			List result = s.execute(sql);
			for (int i = 1; i < result.size(); i++) {
				Vector v = (Vector) result.get(i);
				BaselineVo baselineVo = this.convertVectorToVo(v);
				list.add(baselineVo);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private BaselineVo convertVectorToVo(Vector v) {
		int dlId = ((java.math.BigDecimal) v.get(0)).intValue();
		String paraName = (String) v.get(1);
		float healthy = ((java.math.BigDecimal) v.get(2)).floatValue();
		float notSoHealthy = ((java.math.BigDecimal) v.get(3)).floatValue();
		float needAttention = ((java.math.BigDecimal) v.get(4)).floatValue();

		BaselineVo vo = new BaselineVo();
		vo.setDlId(dlId);
		vo.setParaName(paraName);
		vo.setHealthy(healthy);
		vo.setNotSoHealthy(notSoHealthy);
		vo.setNeedAttention(needAttention);
		return vo;
	}

	private String contructSelectBaseLineSQL() {
		String sql = "SELECT * FROM OPD_BASELINE ORDER BY BL_ID ";
		return sql;
	}

	private String constructUpdateBaseLineSQL(BaselineVo baselineVo) {
		String sqlString = null;
		if (baselineVo != null) {
			sqlString = "update OPD_BASELINE set ";
			sqlString += "HEALTHY='" + baselineVo.getHealthy() + "',";
			sqlString += "NOT_SO_HEALTHY='" + baselineVo.getNotSoHealthy()
					+ "',";
			sqlString += "NEED_ATTENTION='" + baselineVo.getNeedAttention()
					+ "' ";
			sqlString += "where BL_ID='" + baselineVo.getDlId() + "'";
		}
		return sqlString;
	}

	public static void main(String[] args) {
		BaselineService b = new BaselineService();
		BaselineVo baselineVo = new BaselineVo();
		baselineVo.setParaName("SORTING_MEMORY_AREA");
		baselineVo.setHealthy(25);
		baselineVo.setNotSoHealthy(50);
		baselineVo.setNeedAttention(75);
		boolean result = b.configBaseline(baselineVo);
		System.out.println("update: " + result);

		List<BaselineVo> resultList = b.readBaseline();
		for (BaselineVo o : resultList)
			System.out.println(o.getDlId() + " : " + o.getParaName() + " : "
					+ o.getHealthy() + " : " + o.getNotSoHealthy() + " : "
					+ o.getNeedAttention());
	}
}
